package configure;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class OutputConfig {
    //要导出的caseId
    public static String[] CASE_IDS = {};
    //mongo的导出配置
    public static String GET_URL_CASE = "";
    public static String GET_URL_TARGET = "";
    public static String CASE_OUTPUT_FILEPATH = "";
    public static String CASE_OUTPUT_FILE_NAME = "";
    public static String TARGET_OUTPUT_FILEPATH = "";
    public static String TARGET_OUTPUT_FILE_NAME = "";
    public static String Authorization = "";
    //mysql的导出配置
    //JDBC 驱动名及数据库 URL
    public static String DB_URL_SOURCE = "";
    //用户名和密码
    public static String USER_SOURCE = "";
    public static String PASS_SOURCE = "";
    //保存文件路径
    public static String SAVE_FILEPATH = "";
    public static String SAVE_FILENAME = "";
    //oss文件的导出配置
    public static String filePath = "";
    public static String rootPath = "";
    public static String OSS_URL = "";

    public static boolean readOutputConfig() throws Exception {
        File file = new File("output-config.txt");
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));
            String s = null;

            while ((s = br.readLine()) != null) {//使用readLine方法，一次读一行
                if (!s.trim().startsWith("#")) {
                    String key = getKey(s.trim());
                    String value = getValue(s.trim());
                    setKeyAndValue(key, value);
                    System.out.println(key + ":" + value);
                }
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    public static String getKey(String s) {
        String key = "";
        Matcher m = Pattern.compile("(\\[[^\\]]*\\])").matcher(s);
        //获取key
        if (m.find()) {
            key = m.group();
        }
        return key;
    }

    public static String getValue(String s) {
        String value = "";
        Matcher m = Pattern.compile("\"(.*?)\"").matcher(s);
        //获取value
        if (m.find()) {
            value = m.group().substring(1, m.group().length() - 1);
        }
        return value;
    }

    public static void setKeyAndValue(String key, String value) {
        switch (key) {
            case "[CASE_IDS]": CASE_IDS=value.split(","); break;
            case "[mongodb.GET_URL_CASE]":
                GET_URL_CASE = value;
                break;
            case "[mongodb.GET_URL_TARGET]":
                GET_URL_TARGET = value;
                break;
            case "[mongodb.CASE_OUTPUT_FILEPATH]":
                CASE_OUTPUT_FILEPATH = value;
                break;
            case "[mongodb.CASE_OUTPUT_FILE_NAME]":
                CASE_OUTPUT_FILE_NAME = value;
                break;
            case "[mongodb.TARGET_OUTPUT_FILEPATH]":
                TARGET_OUTPUT_FILEPATH = value;
                break;
            case "[mongodb.TARGET_OUTPUT_FILE_NAME]":
                TARGET_OUTPUT_FILE_NAME = value;
                break;
            case "[mongodb.Authorization]":
                Authorization = value;
                break;
            case "[mongodb.DB_URL_SOURCE]":
                DB_URL_SOURCE = value;
                break;
            case "[mongodb.USER_SOURCE]":
                USER_SOURCE = value;
                break;
            case "[mongodb.PASS_SOURCE]":
                PASS_SOURCE = value;
                break;
            case "[mongodb.SAVE_FILEPATH]":
                SAVE_FILEPATH = value;
                break;
            case "[mongodb.SAVE_FILENAME]":
                SAVE_FILENAME = value;
                break;
            case "[mongodb.filePath]":
                filePath = value;
                break;
            case "[mongodb.rootPath]":
                rootPath = value;
                break;
            case "[mongodb.OSS_URL]":
                OSS_URL = value;
                break;
            default:
                break;
        }
    }
    //自动生成
    //public static LinkedList<Long> TARGET_IDS = null;
}
